package com.example.tonyzxb.autoemulator;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Tab Fragment Test
 * @author tony.zxb@alibaba-inc.com
 * @since  2015/12/15 15:23
 */
public class TabFragment extends Fragment {
    @Bind(R.id.tl_title)
    TabLayout tlTitle;
    @Bind(R.id.vp_content)
    ViewPager vpContent;
    private ArrayList<String> titleList;

    public static TabFragment newInstance() {
        
        Bundle args = new Bundle();
        
        TabFragment fragment = new TabFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(this.getActivity(), R.layout.tab_fragment, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        titleList = new ArrayList<>();
        titleList.add("Tab1");
        titleList.add("Tab2");
        titleList.add("Tab3");
        titleList.add("Tab4");
        titleList.add("Tab5");
//        for (String s : titleList) {
//            tlTitle.addTab(tlTitle.newTab().setText(s));
//        }

        CustomPageAdapter customPageAdapter = new CustomPageAdapter();
        vpContent.setAdapter(customPageAdapter);
        tlTitle.setupWithViewPager(vpContent);
        tlTitle.setTabsFromPagerAdapter(customPageAdapter);
    }

    class CustomPageAdapter extends PagerAdapter{


        @Override
        public CharSequence getPageTitle(int position) {
            return titleList.get(position);
        }

        @Override
        public int getCount() {
            return titleList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TextView textView = new TextView(getActivity());
            textView.setBackgroundColor(getActivity().getResources().getColor(android.R.color.holo_red_light));
            textView.setText(titleList.get(position));
            
            container.addView(textView);

            return textView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
//            super.destroyItem(container, position, object);
        }
    } 

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
